using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxScheduler;
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Xml;
using System.Collections;
using System.Data;
using System.Drawing;

public partial class ColorworksDataSources : System.Web.UI.UserControl {
	//bool useTemporaryDataSource = DemoSettings.IsSiteMode;
	bool initAppointments = true;
	string uniqueSessionPrefix = String.Empty;
    bool resourceSharing = false;

	public bool InitAppointments { get { return initAppointments; } set { initAppointments = value; } }
	public string UniqueSessionPrefix { get { return uniqueSessionPrefix; } set { uniqueSessionPrefix = value; } }
	string CustomEventsSessionName { get { return UniqueSessionPrefix + "CustomEvents"; } }
	string CustomResourcesSessionName { get { return UniqueSessionPrefix + "CustomResources"; } }
    public bool ResourceSharing { get { return resourceSharing; } set { resourceSharing = value; } }

	public DataSourceControl AppointmentDataSource {
		get {

				return dbAppointmentDataSource;
		}
	}
	public DataSourceControl ResourceDataSource {
		get {

				return dbResourceDataSource;
		}
	}

	public void AttachTo(ASPxScheduler control) {
		control.ResourceDataSource = this.ResourceDataSource;
		control.AppointmentDataSource = this.AppointmentDataSource;
        SetupStatuses(control);
		control.DataBind();
	}

	protected void Page_Load(object sender, EventArgs e) {
	}

	#region Site Mode implementation
	protected void temporaryAppointmentDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e) {
		CustomEventList events = GetCustomEvents();
		e.ObjectInstance = new CustomEventDataSource(events);
	}
	protected void temporaryResourceDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e) {
		CustomResourceList resources = GetCustomResources();
		e.ObjectInstance = new CustomResourceDataSource(resources);
	}
	public CustomEventList GetCustomEvents() {
		CustomEventList events = Session[CustomEventsSessionName] as CustomEventList;
		if (events != null)
			return events;

        events = InitAppointments ? DemoUtils.CreateCustomEvents(ResourceSharing) : new CustomEventList();
		Session[CustomEventsSessionName] = events;
		return events;
	}
	protected CustomResourceList GetCustomResources() {
		CustomResourceList resources = Session[CustomResourcesSessionName] as CustomResourceList;
		if (resources != null)
			return resources;

        resources = DemoUtils.CreateCustomResurces();		
		Session[CustomResourcesSessionName] = resources;
		return resources;
	}

    
    void SetupStatuses(ASPxScheduler control) {

        control.Storage.Appointments.Statuses.Clear();
        IEnumerable data = dbUsageTypeDataSource.Select(new DataSourceSelectArguments());
        foreach(DataRowView dataItem in data) { 
            string name = (string)dataItem.Row["Name"];
            Color color = GetStatusColor(dataItem.Row["Color"]);
            control.Storage.Appointments.Statuses.Add(GetStatusColor(color), name, name);
        }
    }
    Color GetStatusColor(object cl) {
        if(cl == DBNull.Value)
            return Color.FromArgb(0xFFFFFF);
        if(cl is Color)
            return (Color)cl;
        int statusColor = Convert.ToInt32(cl);
        return Color.FromArgb(statusColor);
    }
	#endregion
}
